<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: zhangjx
 * @Date: 2023-05-29 19:05:02
 * @LastEditors: upholdjx 1373577124@qq.com
 * @LastEditTime: 2023-06-26 17:05:20
-->
<template>
	<div>
		<AplidForm ref="formRef" v-bind="formConfig('form')" v-model="formModel" @submit="onSubmit">
			<template v-slot:parentIdForm>
				<AplidTreeSelect :props="defaultProps" :data="menuTreeData" v-model="formModel.parentId"></AplidTreeSelect>
			</template>
			<div class="fc">
				<el-button v-auth="'sys_menu_edit'" class="ap-btn" type="primary" @click="handleSubmit('submit-next')"> 保存 </el-button>
				<el-button v-auth="'sys_menu_del'" class="ap-btn" type="danger" @click="handleDeleteMenu"> 删除 </el-button>
			</div>
		</AplidForm>
	</div>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import apiMod from '@/apiMod/index'
import { AplidTreeSelect } from '@/components/AplidCrud/index'
import { formConfig } from '../menuHooks'
import { useCrudStore } from '@/stores/crud'
import { storeToRefs } from 'pinia'
const { menuTreeData } = storeToRefs(useCrudStore())
const defaultProps = {
	value: 'id',
	label: 'label',
	children: 'children',
}
const emit = defineEmits(['refresh'])
const props = defineProps({
	nowMenu: Object,
	id: [String, Number],
})
const formRef = ref(null)
const formModel: any = ref(props.nowMenu || {})

const handleDeleteMenu = () => {
	apiMod.sys_menu_del(props.id).then((res) => {
		ElMessage.success('删除成功!')
		emit('refresh', props.nowMenu, 'del')
	})
}
const onSubmit = (form, done) => {
	form.menuId = form.id
	apiMod
		.sys_menu_edit(form)
		.then(() => {
			ElMessage.success('编辑成功!')
			emit('refresh', {}, '')
			done()
		})
		.catch(() => {
			done()
		})
}

const handleSubmit = (type) => {
	formRef.value.onSubmit()
}
</script>

<style scoped></style>
